package com.yylending.las.common.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yylending.las.R;
import com.yylending.las.lending.QuotaEvent;

/**
 * Created by Pengrf on 2017/5/27.
 * Copyright  2017 www.yyfax.com. All Rights Reserved.
 */
public class TelephoneTextView extends android.support.v7.widget.AppCompatTextView implements View.OnClickListener, CommonDialog.ButtonClickListener {

	private String mServicePhone;
	private CommonDialog mCommonDialog;
	/**
	 * 需要弹框的界面编号
	 */
	private int mPageNo;

	OnPhoneNumberClickListener mListener;

	public TelephoneTextView(Context context) {
		super(context);
	}

	public TelephoneTextView(Context context, @Nullable AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public TelephoneTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs);
	}

	private void init(Context context, AttributeSet attrs) {
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TelephoneTextView);
		mServicePhone = ta.getString(R.styleable.TelephoneTextView_service_phone);
		//mCommonDialog = new CommonDialog(getContext(), null, mServicePhone, "呼叫", "取消", this, true);
		setOnClickListener(this);
	}

	public int getPageNo() {
		return mPageNo;
	}

	public void setPageNo(int pageNo) {
		mPageNo = pageNo;
	}

	@Override
	public void onClick(View v) {
		EventAgent.post(new QuotaEvent.ShowCallDialogEvent(QuotaEvent.ShowCallDialogEvent.QUOTA_SHOW_CALL_DIALOG, mPageNo, mServicePhone));
		//mCommonDialog.show();
	}

	@Override
	public void onCancel(CommonDialog dialog) {
		mCommonDialog.dismiss();
	}

	public void setOnPhoneNumberClickListener(OnPhoneNumberClickListener listener) {
		mListener = listener;
	}

	@Override
	public void onConfirm(CommonDialog dialog) {
		mCommonDialog.dismiss();
		if(null != mListener){
			mListener.onPhoneNumberClick(mServicePhone.replace("-",""));
		}
	}

	//点击电话号码
	public interface OnPhoneNumberClickListener {
		void onPhoneNumberClick(String phone);
	}

	public void dismissDialog(){
		if(null != mCommonDialog && mCommonDialog.isShowing()){
			mCommonDialog.dismiss();
		}
		mCommonDialog = null;
	}
}
